<?php
class Master_tapel extends CI_Model 
{

	function __construct()
	{
		parent::__construct();
	}
	
	function insert($tapel){
		
		$data=array('th_ajaran'=>$tapel);
		$insert=$this->db->insert('mr_th_ajaran',$data);
		return $insert;
	}
	
	function insert_periode($id_tapel,$periode,$urutan){
		$data=array('id_tahun_ajaran'=>$id_tapel,
					'periode'=>$periode,
					'urutan'=>$urutan);
		$insert=$this->db->insert('mr_periode_tapel',$data);
		return $insert;
	}
	
	function set_aktif($id,$status){
		$data=array('aktif'=>$status);
		$this->db->where('id_mr_th_ajaran',$id);
		$update=$this->db->update('mr_th_ajaran',$data);
		$sql="update mr_th_ajaran set `aktif`='tidak' where id_mr_th_ajaran<>".$id;
		$query=$this->db->query($sql);
		return $query;
	}
		
	function read($limit, $offset)	{
		$sql="select * from mr_th_ajaran order by th_ajaran desc limit $offset, $limit";
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}
	
	function detail($id){
		$sql="select * from mr_th_ajaran where id_mr_th_ajaran='".$id."'";
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->row_array();
		}else{
			return array();
		}
	}
	
	function detail_periode($id){
		$sql="select * from mr_periode_tapel where id='".$id."'";
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->row_array();
		}else{
			return array();
		}
	}
	
	function tapel_semua(){
		$sql="select * from mr_th_ajaran order by th_ajaran asc";
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}

	function count_all_num_rows(){
		$sql="SELECT * from mr_th_ajaran";
		//echo $sql;
		$query=$this->db->query($sql);
		return $query->num_rows();
	}
	
	
	function delete($id){
		$this->db->where('id_tahun_ajaran', $id);
		$del_periode=$this->db->delete('mr_periode_tapel');
		$this->db->where('id_mr_th_ajaran', $id);
		$delete = $this->db->delete('mr_th_ajaran');
		return $delete;
	}
	
	function get_tapel_aktif(){
		$sql="select * from mr_th_ajaran where aktif='ya'";
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->row_array();
		}else{
			return array();
		}
	}
	
	function get_periode_aktif(){
		$bulan=date('m');
		$tapel_aktif=$this->get_tapel_aktif();
		$id_tapel=$tapel_aktif['id_mr_th_ajaran'];
		$sql="SELECT * FROM mr_periode_tapel WHERE SUBSTR(periode,6,2)='".$bulan."' AND id_tahun_ajaran='".$id_tapel."'";
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->row_array();
		}else{
			return array();
		}
	}
	
	function get_list_periode(){
		$bulan=date('m');
		$tapel_aktif=$this->get_tapel_aktif();
		$id_tapel=$tapel_aktif['id_mr_th_ajaran'];
		$sql="SELECT * FROM mr_periode_tapel WHERE  id_tahun_ajaran='".$id_tapel."'";
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}
	
	function get_periode_tapel($id_tapel){
		$sql="SELECT * FROM mr_periode_tapel WHERE  id_tahun_ajaran='".$id_tapel."'";
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}
	
}